package com.iamteer.contact.dao.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.iamteer.contact.dao.ContactDao;
import com.iamteer.contact.entity.Contact;
import com.iamteer.contact.util.XMLUtil;

public class ContactDaoImpl implements ContactDao {
	private static File file;
	static{
		//需要自己修改“数据库”的路径
		file = new File("/Users/Chuck/Love/Java/Entry/database/contacts.xml");
//		System.out.println(file.exists());
	}
//	public ContactDaoImpl(File file) {
//		this.file = file;
////		System.out.println(file.getAbsolutePath());
//	}
	
	@Override
	public void addContact(Contact contact) {
		try {
//			File file = new File("contacts.xml");
			Document doc;
			Element rootElem;
			if (!file.exists()) {
				doc = DocumentHelper.createDocument();
				rootElem = doc.addElement("contaceList");
			} else {
				doc = new SAXReader().read(file);
				rootElem = doc.getRootElement();
			}
			
			Element contaceElem = rootElem.addElement("contact");
			//系统自动生成随机且唯一的 ID 值，作为编号
			String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
			contaceElem.addAttribute("id", uuid);
			contaceElem.addElement("name").setText(contact.getName());
			contaceElem.addElement("gender").setText(contact.getGender());
			contaceElem.addElement("age").setText(contact.getAge() + "");
			contaceElem.addElement("phone").setText(contact.getPhone());
			contaceElem.addElement("email").setText(contact.getEmail());
			contaceElem.addElement("qq").setText(contact.getQq());
			
			XMLUtil.write2xml(doc, file);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}

	@Override
	public void updateContact(Contact contact) {
		try {
//			File file = new File("contacts.xml");
			Document doc = new SAXReader().read(file);
			
			Element contactElem = (Element)doc.selectSingleNode("//contact[@id='" + contact.getId() + "']");
			contactElem.element("name").setText(contact.getName());
			contactElem.element("gender").setText(contact.getGender());
			contactElem.element("age").setText(contact.getAge() + "");
			contactElem.element("phone").setText(contact.getPhone());
			contactElem.element("email").setText(contact.getEmail());
			contactElem.element("qq").setText(contact.getQq());
			
			XMLUtil.write2xml(doc, file);
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void deleteContact(String id) {
		try {
//			File file = new File("contacts.xml");
			Document doc = new SAXReader().read(file);
			
			Element contactElem = (Element) doc.selectSingleNode("//contact[@id='" + id + "']");
			if (contactElem != null) {
				contactElem.detach();
			}
			
			XMLUtil.write2xml(doc, file);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		
	}

	@Override
	public List<Contact> findAll() {
		try {
//			File file = new File("contacts.xml");
			Document doc = new SAXReader().read(file);
			ArrayList<Contact> list = new ArrayList<Contact>();
			List<Element> conList = (List<Element>)doc.selectNodes("//contact");
			for (Element e : conList) {
				Contact c = new Contact();
				c.setId(e.attributeValue("id"));
				c.setName(e.elementText("name"));
				c.setGender(e.elementText("gender"));
				c.setAge(Integer.parseInt(e.elementText("age")));
				c.setPhone(e.elementText("phone"));
				c.setEmail(e.elementText("email"));
				c.setQq(e.elementText("qq"));
				list.add(c);
			}
			
			return list;
		} catch (DocumentException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public Contact findById(String id) {
		try {
//			File file = new File("contacts.xml");
			Document doc = new SAXReader().read(file);
			Element e = (Element) doc.selectSingleNode("//contact[@id='" + id +"']");
			Contact contact = null;
			if(e != null){
				contact = new Contact();
				contact.setId(e.elementText("id"));
				contact.setName(e.elementText("name"));
				contact.setGender(e.elementText("gender"));
				contact.setAge(Integer.parseInt(e.elementText("age")));
				contact.setPhone(e.elementText("phone"));
				contact.setEmail(e.elementText("email"));
				contact.setQq(e.elementText("qq"));
			}
			return contact;
		} catch (DocumentException e) {
			throw new RuntimeException(e);
		}
	}
}
